<script setup>
import { ref } from 'vue'
import { getUserInfoApi, updateUserApi } from '@/api/user'
import { ElMessage } from 'element-plus'
import { useTokenStore } from '@/store/token'

// 获取登录用户信息
const tokenStore = useTokenStore();

const form = ref({
    userId: tokenStore.user.userId,
    username: '',
    phone: '',
    email: '',
    sex: ''
})
//  修改用户信息时的提交内容
const submitForm = ref({
    userId: '',
    username: '',
    phone: '',
    email: '',
    sex: ''
})

// const rules = ref({
//   username: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
//   email: [{ required: true, message: "邮箱地址不能为空", trigger: "blur" }, { type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"] }],
//   phone: [{ required: true, message: "手机号码不能为空", trigger: "blur" }, { pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur" }],
// });

// 获取用户信息
const getUserInfos = () => {
  console.log(tokenStore.user.userId);
  getUserInfoApi(tokenStore.user.userId).then(res=>{
      console.log(res);
    if (res.code === 0) {
      form.value = res.data[0];
    } else {
      ElMessage.error('获取用户信息失败，请重试')
    }
  })
};
getUserInfos();

/** 保存按钮 */
function submit1() {
  submitForm.value.userId = form.value.userId
  submitForm.value.username = form.value.username
  let data = {
    userId: submitForm.value.userId,
    username: submitForm.value.username
  }
  console.log(data);
  
  updateUserApi(data).then(res=>{
    if(res.code === 0){
      ElMessage.success('修改成功')
    } else {
      ElMessage.error(res.message || '修改失败，请重试')
    }
    getUserInfos();
  })
  .catch(err=>{
      console.log(err);
      ElMessage.error('保存失败，请重试')
  })
}
function submit2() {
  submitForm.value.userId = form.value.userId
  submitForm.value.phone = form.value.phone
  let data = {
    userId: submitForm.value.userId,
    phone: submitForm.value.phone
  }
  console.log(data);
  
  updateUserApi(data).then(res=>{
    if(res.code === 0){
      ElMessage.success('修改成功')
    } else {
      ElMessage.error(res.message || '修改失败，请重试')
    }
    getUserInfos();
  })
  .catch(err=>{
      console.log(err);
      ElMessage.error('保存失败，请重试')
  })
}
function submit3() {
  submitForm.value.userId = form.value.userId
  submitForm.value.email = form.value.email
  let data = {
    userId: submitForm.value.userId,
    email: submitForm.value.email
  }
  console.log(data);
  
  updateUserApi(data).then(res=>{
    if(res.code === 0){
      ElMessage.success('修改成功')
    } else {
      ElMessage.error(res.message || '修改失败，请重试')
    }
    getUserInfos();
  })
  .catch(err=>{
      console.log(err);
      ElMessage.error('保存失败，请重试')
  })
}
function submit4() {
  submitForm.value.userId = form.value.userId
  submitForm.value.sex = form.value.sex
  let data = {
    userId: submitForm.value.userId,
    sex: submitForm.value.sex
  }
  console.log(data);
  
  updateUserApi(data).then(res=>{
    if(res.code === 0){
      ElMessage.success('修改成功')
    } else {
      ElMessage.error(res.message || '修改失败，请重试')
    }
    getUserInfos();
  })
  .catch(err=>{
      console.log(err);
      ElMessage.error('保存失败，请重试')
  })
}
</script>

<template>
  <el-card class="box">
    <p class="title">个人资料</p>
    <div class="user-center">
      <el-form ref="userRef" :model="form" label-width="80px">
          <el-form-item label="用户昵称" prop="username">
              <!-- <el-input v-model="form.username" maxlength="30" />
              <el-button type="primary" @click="submit">保存</el-button> -->
              <el-row :gutter="30">
                <el-col :span="28">
                  <el-input v-model="form.username" />
                </el-col>
                <el-col :span="1" style="display: flex; align-items: center; justify-content: flex-start;">
                  <el-button type="primary" @click="submit1">保存昵称</el-button>
                </el-col>
              </el-row>
          </el-form-item>
          
          <el-form-item label="手机号码" prop="phone">
              <!-- <el-input v-model="form.phone" maxlength="11" /> -->
              <el-row :gutter="30">
                <el-col :span="28">
                  <el-input v-model="form.phone" />
                </el-col>
                <el-col :span="1" style="display: flex; align-items: center; justify-content: flex-start;">
                  <el-button type="primary" @click="submit2">保存手机号</el-button>
                </el-col>
              </el-row>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
              <!-- <el-input v-model="form.email" maxlength="50" /> -->
              <el-row :gutter="30">
                <el-col :span="28">
                  <el-input v-model="form.email" />
                </el-col>
                <el-col :span="1" style="display: flex; align-items: center; justify-content: flex-start;">
                  <el-button type="primary" @click="submit3">保存邮箱</el-button>
                </el-col>
              </el-row>
          </el-form-item>
          <el-form-item label="性别">
              <el-radio-group v-model="form.sex">
                  <el-radio value="男">男</el-radio>
                  <el-radio value="女">女</el-radio>
                  <el-radio value="">保密</el-radio>
              </el-radio-group>
              <el-button type="primary" @click="submit4" style="margin-left: 45px;">保存性别</el-button>
          </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<style scoped lang="scss">
.box{
  padding: 20px;
  min-height: 100%;

  .title{
    font-size: 22px;
    margin-bottom: 10px;

  }
}
.user-center{
  padding: 20px;
  width: 700px;
}


</style>